﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MedicalLog.Domain.Entities;

namespace MedicalLog.Domain.Repositories
{
    public class EmergencyFormRepository
    {
        MedicalLogEntities _dbMedicalLog;

        public EmergencyFormRepository()
        {
            _dbMedicalLog = new MedicalLogEntities();
        }

        /// <summary>
        /// This method allows you to add an emergency form to the database
        /// </summary>
        /// <param name="newEmergencyForm"></param>
        public void AddEmergencyForm(EmergencyForm newEmergencyForm)
        {
            _dbMedicalLog.AddToEmergencyForms(newEmergencyForm);
            _dbMedicalLog.SaveChanges();
        }

        /// <summary>
        /// This method allows to delete an existing emergency form
        /// </summary>
        /// <param name="Id"></param>
        public void DeleteEmergencyForm(int Id)
        {
            var emerForm = _dbMedicalLog.EmergencyForms.First(x => x.Id == Id);
            if (emerForm != null)
            {
                _dbMedicalLog.EmergencyForms.DeleteObject(emerForm);
                _dbMedicalLog.SaveChanges();
            }
        }

        /// <summary>
        /// This method allows you to see the detail of the patient
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public EmergencyForm DetailEmergencyForm(int Id)
        {
            return _dbMedicalLog.EmergencyForms.First(x => x.Id == Id);
        }

        /// <summary>
        /// This method allows you to update an emergency form.
        /// </summary>
        /// <param name="oldEmergencyForm"></param>
        //public void UpdateEmergencyForm(EmergencyForm oldEmergencyForm)
        //{
        //    _dbMedicalLog.EmergencyForms.FirstOrDefault(x => x.Id == oldEmergencyForm.Id);
        //    _dbMedicalLog.EmergencyForms.ApplyCurrentValues(oldEmergencyForm);
        //    _dbMedicalLog.SaveChanges();
        //}

        /// <summary>
        /// This method allows you to update an emergency form.
        /// </summary>
        /// <param name="oldEmergencyForm"></param>
        public void UpdateEmergencyForm(EmergencyForm oldEmergencyForm)
        {
            EmergencyForm EmerForm = _dbMedicalLog.EmergencyForms.FirstOrDefault(x => x.Id == oldEmergencyForm.Id);
            EmerForm.PhysicalTest = oldEmergencyForm.PhysicalTest;
            EmerForm.SocialHistory = oldEmergencyForm.SocialHistory;
            EmerForm.Allergies = oldEmergencyForm.Allergies;
            EmerForm.Date = oldEmergencyForm.Date;
            EmerForm.Diagnostic = oldEmergencyForm.Diagnostic;
            EmerForm.Disposition = oldEmergencyForm.Disposition;
            EmerForm.DispositionDescription = oldEmergencyForm.DispositionDescription;
            EmerForm.FormCompleted = oldEmergencyForm.FormCompleted;
            EmerForm.HistoryActualDisease = oldEmergencyForm.HistoryActualDisease;
            EmerForm.Symptoms = oldEmergencyForm.Symptoms;
            EmerForm.SystemRevision = oldEmergencyForm.SystemRevision;
            EmerForm.Treatment = oldEmergencyForm.Treatment;
            _dbMedicalLog.EmergencyForms.ApplyCurrentValues(EmerForm);
            _dbMedicalLog.SaveChanges();
        }

        /// <summary>
        /// This method allows you to extract a doctor from the database
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>
        public Doctor getDoctor(int pId)
        {
            return _dbMedicalLog.Doctors.First(x => x.IdDoctor == pId);
        }

        /// <summary>
        /// This method allows you to extract a patient from a database
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>
        public Patient getPatient(int pId)
        {
            return _dbMedicalLog.Patients.First(x => x.Id == pId);
        }

        /// <summary>
        /// This method allows you to obtain a Test List from the database
        /// </summary>
        /// <param name="pName"></param>
        /// <returns></returns>
        public TestList getTestList(string pName)
        {
            return _dbMedicalLog.TestLists.First(x => x.TestName == pName);
        }

        /// <summary>
        /// This method allows you to add a Lab Test from the database
        /// </summary>
        /// <param name="pLabTest"></param>
        public void AddLabTest(LabTest pLabTest)
        {
            _dbMedicalLog.LabTests.AddObject(pLabTest);
            _dbMedicalLog.SaveChanges();
        }

        /// <summary>
        /// This method allows you to get a Vital Sign List from the database
        /// </summary>
        /// <param name="pName"></param>
        /// <returns></returns>
        public VitalSingList getVitalSignList(string pName)
        {
            return _dbMedicalLog.VitalSingLists.First(x => x.VitalSignName == pName);
        }

        /// <summary>
        /// This method allows you to add vital sign list to the database, this method is here because i need the same dbcontext 
        /// object to relate entities
        /// </summary>
        /// <param name="pVitalSign"></param>
        public void AddVitalSign(VitalSing pVitalSign)
        {
            _dbMedicalLog.VitalSings.AddObject(pVitalSign);
            _dbMedicalLog.SaveChanges();
        }

        /// <summary>
        /// This method allows you to get the 10 first diagnostig from the database
        /// </summary>
        /// <param name="pText"></param>
        /// <returns></returns>
        public IQueryable<string> getDiagnostic(string pText)
        {
            return _dbMedicalLog.EmergencyForms.Where(x => x.Diagnostic.StartsWith(pText)).Select(x => x.Diagnostic)
                .Distinct().Take(10);
        }

        /// <summary>
        /// This method allows you to edit an labtest, this method is here because i need the same dbcontext object to relate entities
        /// </summary>
        /// <param name="pLabTest"></param>
        public void UpdateLabTest(LabTest pLabTest, EmergencyForm pEmergencyForm, string pValue)
        {
            if (pLabTest.Id != 0)
            {
                LabTest Test = _dbMedicalLog.LabTests.First(x => x.Id == pLabTest.Id);
                Test.Date = pLabTest.Date;
                Test.Description = pLabTest.Description;
                _dbMedicalLog.LabTests.ApplyCurrentValues(Test);
                _dbMedicalLog.SaveChanges();
            }
            else
            {
                pLabTest.EmergencyForm = _dbMedicalLog.EmergencyForms.First(x => x.Id == pEmergencyForm.Id);
                pLabTest.TestList = _dbMedicalLog.TestLists.First(x => x.TestName == pValue);
                _dbMedicalLog.LabTests.AddObject(pLabTest);
                _dbMedicalLog.SaveChanges();
            }
        }

        public void UpdateVitalSign(VitalSing pVitalSign, EmergencyForm pEmergencyForm, string pValue)
        {
            if (pVitalSign.Id != 0)
            {
                VitalSing VS = _dbMedicalLog.VitalSings.First(x => x.Id == pVitalSign.Id);
                VS.Date = pVitalSign.Date;
                VS.Description = pVitalSign.Description;
                _dbMedicalLog.VitalSings.ApplyCurrentValues(VS);
                _dbMedicalLog.SaveChanges();
            }
            else
            {
                pVitalSign.EmergencyForm = _dbMedicalLog.EmergencyForms.First(x => x.Id == pEmergencyForm.Id);
                pVitalSign.VitalSingList = _dbMedicalLog.VitalSingLists.First(x => x.VitalSignName == pValue);
                _dbMedicalLog.VitalSings.AddObject(pVitalSign);
                _dbMedicalLog.SaveChanges();
            }
        }
    }
}
